package com.healthai.controller;

import com.healthai.dto.request.DailyRecordRequest;
import com.healthai.dto.response.ApiResponse;
import com.healthai.entity.DailyRecord;
import com.healthai.entity.User;
import com.healthai.service.DailyRecordService;
import com.healthai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/daily")
public class DailyRecordController {
    
    @Autowired
    private DailyRecordService dailyRecordService;
    @Autowired
    private UserService userService;
    /**
     * 获取当前用户ID
     * @return 用户ID
     */
    private Integer getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        // 这里需要从用户名获取用户ID，可以通过UserService实现
        User user = userService.getUserByUsername(username);
        return user.getUserId();
    }
    
    /**
     * 保存每日记录
     * @param request 每日记录请求
     * @return 记录ID
     */
    @PostMapping("/record")
    public ApiResponse<Integer> saveDailyRecord(@Validated @RequestBody DailyRecordRequest request) {
        request.setRecordDate(LocalDate.now());
        Integer userId = getCurrentUserId();
        System.out.println(userId);
        Integer recordId = dailyRecordService.saveDailyRecord(userId, request);
        return ApiResponse.success("保存成功", recordId);
    }
    
    /**
     * 获取某日期的每日记录
     * @param date 日期
     * @return 每日记录
     */
    @GetMapping("/record")
    public ApiResponse<DailyRecord> getDailyRecordByDate(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        Integer userId = getCurrentUserId();
        DailyRecord record = dailyRecordService.getDailyRecordByDate(userId, date);
        return ApiResponse.success(record);
    }
    
    /**
     * 获取日期范围内的每日记录
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 每日记录列表
     */
    @GetMapping("/records")
    public ApiResponse<List<DailyRecord>> getDailyRecordsByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Integer userId = getCurrentUserId();
        List<DailyRecord> records = dailyRecordService.getDailyRecordsByDateRange(userId, startDate, endDate);
        return ApiResponse.success(records);
    }
    
    /**
     * 获取最近的每日记录
     * @param limit 限制数量
     * @return 每日记录列表
     */
    @GetMapping("/records/recent")
    public ApiResponse<List<DailyRecord>> getRecentDailyRecords(
            @RequestParam(defaultValue = "10") Integer limit) {
        Integer userId = getCurrentUserId();
        List<DailyRecord> records = dailyRecordService.getRecentDailyRecords(userId, limit);
        return ApiResponse.success(records);
    }
    
    /**
     * 删除每日记录
     * @param recordId 记录ID
     * @return 是否成功
     */
    @DeleteMapping("/record/{recordId}")
    public ApiResponse<Boolean> deleteDailyRecord(@PathVariable Integer recordId) {
        Integer userId = getCurrentUserId();
        boolean success = dailyRecordService.deleteDailyRecord(userId, recordId);
        return ApiResponse.success("删除成功", success);
    }
}